//
//  ViewController.swift
//  UIDynamics创建运动视图
//
//  Created by quanhao huang on 2019/9/27.
//  Copyright © 2019 demo. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var animator : UIDynamicAnimator?
    @IBOutlet weak var animatorView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.animator = UIDynamicAnimator(referenceView: self.view)
        
        /*为视图添加重力*/
        let gravity = UIGravityBehavior(items: [self.animatorView])
        self.animator?.addBehavior(gravity)
        /*添加碰撞(按钮会落到底部l停留)*/
        let collision = UICollisionBehavior(items: [self.animatorView])
//        collision.translatesReferenceBoundsIntoBoundary = true//这样会直接掉到容器边缘，若想把触碰边缘放到里边则调用下边的方法
        collision.setTranslatesReferenceBoundsIntoBoundary(with: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
        self.animator?.addBehavior(collision)
        /*添加附着物*/
        //瞄点位于屏幕上端的中央位置
        let anchor = CGPoint(x: self.view.bounds.width/2, y: 0)
        let attachment = UIAttachmentBehavior(item: self.animatorView, attachedToAnchor: anchor)
        self.animator?.addBehavior(attachment)
    }


}

